package com.yt.server.comm;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yt.server.AttributeStore;
import com.yt.server.Context;
import com.yt.server.action.ActionProxy;
import com.yt.server.api.IConnection;
import com.yt.server.api.IScope;
import com.yt.server.api.action.IActionInvoker;
import com.yt.server.api.action.IActionProxy;

/**
 * 连接基类
 */
public abstract class BaseConnection extends AttributeStore implements
		IConnection {

	private static final Logger log = LoggerFactory
			.getLogger(BaseConnection.class);

	protected volatile String host;

	protected volatile String remoteAddress;

	protected volatile List<String> remoteAddresses;

	protected volatile int remotePort;

	protected volatile String path;

	protected volatile IScope scope; // 如果为空,为游离状态.

	protected AtomicLong readMessages = new AtomicLong(0);

	protected AtomicLong writtenMessages = new AtomicLong(0);

	protected volatile Map<String, Object> params = null;

	protected IActionProxy proxy = new ActionProxy(this);// 代理

	protected IActionInvoker actionInvoker = Context.getActionInvoker();// 调用者

	private final ReadWriteLock lock = new ReentrantReadWriteLock();

	protected Set<IScope> basicScopes = new CopyOnWriteArraySet<IScope>();

	public BaseConnection() {
	}

	public BaseConnection(Map<String, Object> params) {
		this.params = params;
	}

	@Override
	public IActionProxy getProxy() {
		return proxy;
	}

	public Lock getReadLock() {
		return lock.readLock();
	}

	public Lock getWriteLock() {
		return lock.writeLock();
	}

	@Override
	public String getHost() {
		return host;
	}

	@Override
	public String getRemoteAddress() {
		return remoteAddress;
	}

	@Override
	public List<String> getRemoteAddresses() {
		return remoteAddresses;
	}

	@Override
	public int getRemotePort() {
		return remotePort;
	}

	@Override
	public String getPath() {
		return scope != null ? scope.getPath() : null;
	}

	@Override
	public Map<String, Object> getConnectParams() {
		return Collections.unmodifiableMap(params);
	}

	@Override
	public IScope getScope() {
		return scope;
	}

	@Override
	public void setScope(IScope scope) {
		this.scope = scope;
	}

	@Override
	public long getReadMessages() {
		return readMessages.get();
	}

	@Override
	public long getWrittenMessages() {
		return writtenMessages.get();
	}

	@Override
	public Iterator<IScope> getScopes() {
		return basicScopes.iterator();
	}

	@Override
	public void registerScope(IScope scope) {
		// basicScopes.add(scope);
		// scope.addEventListener(this);
	}

	@Override
	public void unregisterScope(IScope scope) {
		// basicScopes.remove(scope);
		// scope.removeEventListener(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		if (host != null) {
			result = prime * result + host.hashCode();
		}
		if (remoteAddress != null) {
			result = prime * result + remoteAddress.hashCode();
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		BaseConnection other = (BaseConnection) obj;
		if (host != null && !host.equals(other.getHost())) {
			return false;
		}
		if (remoteAddress != null
				&& !remoteAddress.equals(other.getRemoteAddress())) {
			return false;
		}
		return true;
	}
}